package com.kai.awt.menu;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @Author Mr.Yan
 * @create 2022/3/20 15:56
 * @description:
 */
public class Demo2 {
    public static void main(String[] args) {
        Frame frame = new Frame("pMenu");
        TextArea textArea = new TextArea(30, 30);
        frame.add(textArea,BorderLayout.SOUTH);

        PopupMenu popupMenu = new PopupMenu();

        MenuItem menuItem1 = new MenuItem("复制");
        MenuItem menuItem2 = new MenuItem("粘贴");
        menuItem1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.append(e.getActionCommand()+"\n");
            }
        });
        menuItem2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.append(e.getActionCommand()+"\n");
            }
        });
        popupMenu.add(menuItem1);
        popupMenu.add(menuItem2);

        Panel panel = new Panel();
        panel.setBounds(0,0,300,300);
        panel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()){
                    popupMenu.show(panel,e.getX(),e.getY());

                }
            }
        });
        panel.add(popupMenu);

        frame.add(panel);

        frame.pack();
        frame.setVisible(true);

    }
}
